/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package collections;

import model.SimpleCollection;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

/**
 * Contains tests of SimpleCollection
 * @author Dmitri Paziy
 */
public class SimpleCollectionTest {
    private SimpleCollection<String> collection;
    
    public SimpleCollectionTest() {
    }
    
    @Before
    public void setUp() {
        collection = new SimpleCollection<String>();
    }
    
    @After
    public void tearDown() {
    }
    
    @Test(expected=IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionWhenPassNegativeCapacityToConstructor() throws Exception{
        //given
        int negativeCapacity = -1;
        //when
        collection = new SimpleCollection<String>(negativeCapacity);
    }
    
    @Test
    public void testCollectionCapacity() throws Exception{
        //given
        int expected = collection.initialCapacity;
        //when
        //then
        assertEquals(expected, collection.capacity());
    }
    
    @Test
    public void shouldIncreaseCollectionSizeWhenAdd() throws Exception{
        //given
        int expected = 1;
        //when
        collection.add("String");
        //then
        assertEquals(expected, collection.size());
    }
    
    @Test(expected=IndexOutOfBoundsException.class)
    public void shouldThrowIndexOutOfBoundsExceptionWhenGetUnexistingElement() throws Exception{
        collection.get(0);
    }
    
    @Test(expected=NullPointerException.class)
    public void shouldThrowNullPointerExceptionWhenAddNullElement() throws Exception{
        collection.add(null);
    }
    
    @Test
    public void shouldAddElementToCollectionWhenAdd() throws Exception{
        //given
        String expected = "String";
        //when
        collection.add(expected);
        //then
        assertEquals(expected, collection.get(0));
    }
    
    @Test
    public void shouldBeEmptyWhenRemoveLastElement() throws Exception{
        //given
        int expected = 0;
        //when
        collection.add("String");
        collection.remove(0);
        //then
        assertEquals(expected, collection.size());
    }
    
    @Test
    public void shouldArrayWhenToArray() throws Exception{
        //given
        String[] expected = {"String1", "String2"};
        //when
        collection.add(expected[0]);
        collection.add(expected[1]);
        //then
        Assert.assertArrayEquals(expected, collection.toArray());
    }
    
    @Test
    public void shouldDoubleCapacityWhenAddMaximumAmountOfElements() throws Exception{
        //given
        String string = "String1";
        int expected = collection.initialCapacity * 2;
        //when
        for (int index = 0; index <= collection.initialCapacity; index++) {
            collection.add(string);
        }
        //then
        assertEquals(expected, collection.capacity());
    }
}
